package me.helllp.book.jvm.ch02;

import java.lang.reflect.Field;

/**
 * 直接内存溢出
 * VM Args：-Xmx20M -XX:MaxDirectMemorySize=10M
 * 
 * @author linxm
 *
 */
public class DirectMemoryOOM {
    private static final int _1Mb = 1024 * 1024;
    
    @SuppressWarnings("restriction")
    public static void main(String[] args) throws IllegalArgumentException, IllegalAccessException {
        Field unsafeField = sun.misc.Unsafe.class.getDeclaredFields()[0];
        
        unsafeField.setAccessible(true);
        sun.misc.Unsafe unsafe = (sun.misc.Unsafe)unsafeField.get(null);
        
        while(true){
            unsafe.allocateMemory(_1Mb);
        }
    }
}